package com.lxt.blog.dao;

import com.lxt.blog.pojo.User;
import com.lxt.blog.util.DBUtil;

import java.sql.*;

public class UserDaoImpl implements UserDao{
    @Override
    public boolean selectUser(String userId, String password) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet result = null;
        int isExist = 0;
        try{
            conn = DBUtil.getConnection();
            if(password == null){
                String sql = "select count(*) as user from t_login where userid = ?";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1,userId);
            } else {
                String sql = "select count(*) as user from t_login where userid = ? and password = ?";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1,userId);
                stmt.setString(2,password);
            }
            result = stmt.executeQuery();
            result.next();
            isExist = result.getInt("user");
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,stmt,result);
        }
        return isExist == 1 ? true : false;
    }

    @Override
    public String selectUserPassword(String userId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet result = null;
        StringBuilder password = new StringBuilder();
        try{
            conn = DBUtil.getConnection();
            String sql = "select password from t_login where userid = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,userId);
            result = stmt.executeQuery();
            result.next();
            password.append(result.getString(1));
            //如果密码查询到，状态为1，否则为0

        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,stmt,result);
        }
        return password.toString();
    }

    @Override
    public boolean insertUser(User user) {
        int insertOK = 0;
        Connection conn = null;
        PreparedStatement stmt = null;
        int isExist = 0;
        try{
            //写入用户表
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into t_user values(null,?,?,?,?,?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,user.getUserId());
            stmt.setString(2,user.getPassword());
            stmt.setString(3,user.getSex());
            stmt.setString(4,user.getHobby());
            stmt.setString(5,user.getIntroduce());
            insertOK = stmt.executeUpdate();
            String loginSql = "insert into t_login values(null,?,?)";
            //写入登录表
            stmt = conn.prepareStatement(loginSql);
            stmt.setString(1,user.getUserId());
            stmt.setString(2,user.getPassword());
            stmt.executeUpdate();
            conn.commit();
        } catch (SQLException e){
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,stmt,null);
        }
        return insertOK == 1 ? true : false;
    }
}
